package br.mestrado.unb.bs.model.configuration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import br.mestrado.unb.bs.model.sensor.Payload;


public class SensorConfiguration {

	private Map<SensorIdentification, Payload> config;
	
	public Map<SensorIdentification, Payload> getConfig() {
		return config;
	}
	
	public List<SensorIdentification> getSensor(Class<? extends Payload> clazz){
		List<SensorIdentification> sis = new ArrayList<SensorIdentification>();
		synchronized (config) {
			Set<Entry<SensorIdentification, Payload>> entrySet = config.entrySet();
			for (Entry<SensorIdentification, Payload> entry : entrySet) {
				if(entry.getValue().getClass().equals(clazz)){
					sis.add(entry.getKey());
				}
			}
		}
		return sis;
	}
	

	public void add(SensorIdentification id, Payload payload){
		if(this.config==null){
			this.config = new HashMap<SensorIdentification, Payload>();
		}
		synchronized (config) {
			this.config.put(id, payload);
		}
	}
	
	public void update(SensorIdentification id, Payload payload){
		synchronized (config) {
			if(this.config.containsKey(id)){
				this.config.put(id,payload);
			}
		}
	}
	
	
	
	public Payload getConfiguration(SensorIdentification id){
		synchronized (config) {
			if(this.config.containsKey(id)){
				return this.config.get(id);
			}
		}
		return null;
	}
	
}

